package cn.springmvc.controller;
 
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.springmvc.model.ResultBaseBean;
import cn.springmvc.model.entity.UserBean;
import cn.springmvc.service.IUserService;
import cn.springmvc.util.SessionManager;
 
@Controller
@RequestMapping(value="/rest")
public class UserController{
	private static final Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	private IUserService userService;
	
    @RequestMapping(value="/user", method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<UserBean> addUser(@RequestBody UserBean userBean){
    	ResultBaseBean<UserBean> resultBaseBean = userService.insert(userBean);
    	return resultBaseBean;
    }
    
    /*
    @RequestMapping(value = "/user/{id}",method=RequestMethod.GET)
    public @ResponseBody ResultBaseBean<UserBean> getOneUser(@PathVariable Integer id){
    	UserBean userBean = new UserBean();
    	userBean.setUserId(id);
    	ResultBaseBean<UserBean> resultBaseBean = userService.selectOne(userBean);
    	return resultBaseBean;
    }
    */
    
    @RequestMapping(value="/user",method=RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<UserBean> getUserList(@RequestParam("sessionId") String sessionId){
    	ResultBaseBean<UserBean> resultBaseBean;
    	UserBean userBean = new UserBean();
    	
		SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			resultBaseBean = userService.select(userBean);
		}else{
			resultBaseBean = new ResultBaseBean<UserBean> ();
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("请先登录");
		}
	
    	return resultBaseBean;
    }
    
    @RequestMapping(value="/user",method=RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<UserBean> updateUser(@RequestBody UserBean userBean,@RequestParam("sessionId") String sessionId){
    	logger.info("Update user with id: " + userBean.getUserId());
    	
    	ResultBaseBean<UserBean> resultBaseBean;
    	
		SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			resultBaseBean = userService.update(userBean);
		}else{
			resultBaseBean = new ResultBaseBean<UserBean> ();
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("请先登录");
		}
		
    	return resultBaseBean;
    }
    
    @RequestMapping(value="/user",method=RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<UserBean> deleteUser(@RequestBody UserBean userBean, @RequestParam("sessionId") String sessionId){
    	
    	ResultBaseBean<UserBean> resultBaseBean;
    	
		SessionManager sessionManager = SessionManager.getInstance();
		HttpSession httpSession = sessionManager.getSessionById(sessionId);
		
		if(httpSession != null){
			resultBaseBean = userService.delete(userBean);
		}else{
			resultBaseBean = new ResultBaseBean<UserBean> ();
			resultBaseBean.setSuccess(false);
			resultBaseBean.setMessage("请先登录");
		}
    	
    	return resultBaseBean;
    }
    
    @RequestMapping(value="/login",method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<UserBean> login(HttpSession httpSession, @RequestBody UserBean userBean){
    	ResultBaseBean<UserBean> resultBaseBean = userService.login(httpSession, userBean);
    	return resultBaseBean;
    }
    
    @RequestMapping(value="/logout",method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody ResultBaseBean<UserBean> logout(@RequestParam("sessionId") String sessionId){
    	ResultBaseBean<UserBean> resultBaseBean = userService.logout(sessionId);
    	return resultBaseBean;
    }
     
}